<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title><?=L('login_title')?></title>
<meta name="robots" content="noindex,nofollow">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel='stylesheet' href='<?=Web_Path?>packs/layui/css/layui.css'>
<link rel="stylesheet" href="<?=Web_Path?>packs/webupload/plupload.css" />
<script src="<?=Web_Path?>packs/js/jquery.min.js"></script>
<script src="<?=Web_Path?>packs/layui/layui.js"></script>
<script type="text/javascript" src="<?=Web_Path?>packs/webupload/plupload.full.min.js"></script>
<script type="text/javascript">
var uploader,len = 0,upnum = <?=$nums?>,up_params = <?=$params?>;
$(document).ready(function(){
	uploader = new plupload.Uploader({ 
		runtimes: 'html5,flash,silverlight,html4',
		browse_button: 'addnew',
		url: "<?=$upsave?>",
		multi_selection: upnum == 1 ? false : true,
		flash_swf_url: '<?=Web_Path?>packs/webupload/Moxie.swf',
		silverlight_xap_url: '<?=Web_Path?>packs/webupload/Moxie.xap',
		filters: {
			max_file_size: '<?=$size?>',
			mime_types: [{title: "files", extensions: "<?=$types?>"}],
			prevent_duplicates : true
		},
		init: {  
			FilesAdded: function(up, files) {
				var li = '',index = 1,no = 0;
				plupload.each(files, function(file) {
					if(index > upnum || (upnum==1 && len>0)){
						up.removeFile(file);
						no++;
					}else{
						li += '<div class="progressWrapper" id="' + file['id'] + '"><div class="progressContainer"><a class="progressCancel" onclick="get_updel(\'' + file['id'] + '\');" href="#" upid="' + file['id'] + '" style="visibility: visible;"> </a><div class="progressName">'+file['name']+'</div><div class="progressBarStatus">&nbsp;</div><div class="progressBarInProgress"></div></div></div>';
						len++;
					}
					index++;
				});
				$("#fsUploadProgress").append(li); 
				if(no>0){
					parent.cscms.layer.msg('您最多只能上传'+upnum+'个文件',{icon:2});
				}
			},
			BeforeUpload: function(up, file) {
				get_params(up,file.name);
			},
			UploadProgress: function(up, file) {
				var percent = file.percent;  
				if(percent == 100){
					$("#" + file.id).find(".progressBarStatus").text("上传完成，请稍后..");
				}else{
					$("#" + file.id).find('.progressBarInProgress').css({"width": percent + "%"});  
					$("#" + file.id).find(".progressBarStatus").text("正在上传("+percent + " %)请稍后..");
				}
			},
			FileUploaded: function(up, file, info) {
				if(up_params.upkey === undefined){
					var fhost = '<?=$fhhost?>';
					var fileurl = fhost + up_params.key;
					var msg = 'ok';
				}else{
					var data = JSON.parse(info.response);
					var msg = unescape(data['msg']);
					var fileurl = data['fileurl'];
				}
				get_ok(msg,fileurl);
			},
			Error: function(up, err) {
				console.log(err.response);
				if (err.code == -600) {
					parent.cscms.layer.msg("选择的文件太大了",{icon:2});
				} else if (err.code == -601) {
					parent.cscms.layer.msg("选择的文件后缀不对",{icon:2});
				} else if (err.code == -602) {
					parent.cscms.layer.msg("这个文件已经上传过了",{icon:2});
				} else {
					parent.cscms.layer.msg(err.response,{icon:2});
				}
				location.reload();
			}
		}
	});
	function get_params(up,filename){
		var pos = filename.lastIndexOf('.');
		var suffix = '.jpg';
		if (pos != -1) {
			suffix = filename.substring(pos);
		}
		up_params.key = '<?=$dir?>' + random_string() + suffix;
		new_multipart_params = up_params;
		delete new_multipart_params.host;
		up.setOption({
			'multipart_params': new_multipart_params
		});
		return false;
	};
	uploader.init();
});
function get_updel(_id){
    var filebnj = uploader.getFile(_id);
	$("#" + _id).remove();
	len--;
	uploader.removeFile(filebnj);
}
function random_string(n) {
　　n = n || 32;
　　var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';   
　　var maxPos = chars.length;
　　var pwd = '';
　　for (i = 0; i < n; i++) {
    　　pwd += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}
function get_ok(msg,fileurl){
	var sid = <?=$sid?>;
	if(msg=='ok'){
		 if(sid==0){
			parent.$('#<?=$fhid.$fid?>').val(fileurl);
		 }else{
			var text=parent.$('#<?=$fhid.$fid?>').val();
			 if(text!='') text+="\n";
			parent.$('#<?=$fhid.$fid?>').val(text+fileurl);
		}
		var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
		parent.layer.close(index); //再执行关闭   
	} else {
		 if(msg=='no') msg='上传失败~';
		 parent.cscms.layer.msg(msg,{icon:2});
		 location.reload();
	}
}
</script>
</head>
<body id="upload_up" style="padding: 0px 10px">
<div class="layui-tab" lay-filter="demo">
	<ul class="layui-tab-title">
		<li class="layui-this"><?=L('tpl_u_02')?></li>
		<li><?=L('tpl_u_03')?></li>
		<li><?=L('tpl_u_04')?></li>
	</ul>
	<div class="layui-tab-content">
		<div class="layui-tab-item layui-show">
			<div id="cnt_0" class="dr_hide">
				<div>
					<div class="addnew" id="addnew">
						<span id="buttonPlaceHolder"></span>
					</div>
					<input type="button" id="btupload" value="<?=L('tpl_u_05')?>" onClick="uploader.start();" />
					<div id="nameTip" class="onShow"> <?=L('tpl_u_06',array($nums,ceil($size/1024)))?></div>
					<div class="bk3"></div>
					<div class="lh24"><?=L('tpl_u_07').$types?></div>
				</div> 	
				<div class="bk10"></div>
				<fieldset class="blue pad-10" id="swfupload">
				<legend><?=L('tpl_u_08')?></legend>
				<ul class="attachment-list" id="fsUploadProgress">    
				</ul>
				</fieldset>
			</div>
		</div>
		<div class="layui-tab-item">
			<div class="layui-form-item">
			    <label class="layui-form-label" style="width: 60px;"><?=L('tpl_u_09')?></label>
			    <div class="layui-input-inline" style="margin-left: 0px;width: 250px;">
			        <input type="text" name="url" id="url" class="layui-input" value="http://">
			    </div>
			    <div class="layui-input-inline" style="margin-left: 0px;width: auto;">
			        <input onclick="get_ok('ok',$('#url').val());" class="layui-btn" type="button" name="button" value="提交">
			    </div>
			</div>
		</div>
		<div class="layui-tab-item">
			<div id="cnt_2" class="dr_hide">
				<iframe name="myfile" id="myfile" src="<?=site_url('upload/myattach')?>?ext=<?=str_replace(',','|', $types)?>" frameborder="false" scrolling="auto" style="overflow-x:hidden;border:none;height: 210px" width="100%" allowtransparency="true" id="myattach"></iframe>
			</div>
		</div>
	</div>
</div>
<script src="<?=Web_Path?>packs/admin/js/cscms.js"></script>
<script>
cscms.init();
$(document).ready(function() {
	setInterval(function(){
		$('#myfile').css('height', '210px');
	},300);
});
</script>
</body>
</html>